package com.example.qcrmdemo.Controller;

import com.example.qcrmdemo.entity.SalesChannces;
import com.example.qcrmdemo.service.SalesChanncesService;
import com.github.pagehelper.PageInfo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
public class SalesChanncesController {
    private static final Logger logger = LoggerFactory.getLogger(SalesChanncesController.class);

    @Autowired
    private SalesChanncesService salesChanncesService;

    @GetMapping("/YingXiaoGuanLi/salesChannces")
    public ModelAndView salesChanncesView(
            @RequestParam(name = "pageNum",required = true,defaultValue = "1") int pageNum,
            @RequestParam(name = "pageSize",required = true,defaultValue = "5") int pageSize,
            @RequestParam(name = "keyword",required = false) String keyword
    ){

        ModelAndView mav = new ModelAndView("YingXiaoGuanLi/salesChannces");
//        PageInfo<SalesChannces> page = salesChanncesService.selectAll();
//        mav.addObject("page",page);
        return mav;
    }

    @GetMapping("/YingXiaoGuanLi/salesChannces-new")
    public ModelAndView salesChanncesNewView(){
        ModelAndView mav = new ModelAndView("YingXiaoGuanLi/salesChannces-new");
        return mav;
    }

    @PostMapping("/YingXiaoGuanLi/salesChannces-new")
    //@Validated注解所标注的参数，表明该参数中的属性取值必须要按照属性上定义的验证规则进行验证，验证是有验证框架完成的
    //验证的结果会以BindingResult实例的形式传入到该方法中
    public ModelAndView salesChanncesNew(@Validated SalesChannces salesChannces, BindingResult bindingResult){
        //redirect表示重定向响应
        //给浏览器仅发送一个请求地址，而没有任何视图内容或数据
        //浏览器会使用收到的地址，重新发送一个Get请求，由于是新请求，因此没有缓存上一次请求的数据，从而达到防止重复提交表单的目的
        ModelAndView mav = new ModelAndView("redirect:/YingXiaoGuanLi/salesChannces-new");
        Map<String,String> errorMessage = new HashMap<>();
        if(bindingResult.hasErrors()){
            //如果有验证错误信息，那么要进行处理
            //获得出现错误的域，以及相应错误消息文本
            final List<FieldError> fieldErrors = bindingResult.getFieldErrors();
            //将错误信息放入到映射中
            for (FieldError error : fieldErrors){
                //从每一个错误域对象中取出域名称，错误信息，加到映射集合中
                errorMessage.put(error.getField(),error.getDefaultMessage());
            }
            //将错误信息发送给视图
            mav.addObject("errorMessage",errorMessage);
            //将客户填写的信息
//            Date date = new Date();//获取系统时间
//            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
//            String nowTime = sdf.format(date);
//            Date time = sdf.parse(nowTime);

            mav.addObject("salesChannces",salesChannces);
            //由于没有保存数据，因此不必进行重定向，内部转发请求到视图，同时，还要以保留请求数据，以及相应的信息
            mav.setViewName("YingXiaoGuanLi/salesChannces-new");
        }else {
            //如果没有错误信息，验证通过，保存客户信息
            //调用服务方法保存客户数据
            Date time = new java.util.Date(new java.util.Date().getTime());
            salesChannces.setActTime(time);

            salesChannces.setCreateTime(time);
            boolean result = salesChanncesService.append(salesChannces);

            if (result){
                logger.info("新增销售机会信息成功 - " + salesChannces);
            }else {
                logger.info("新增销售机会信息失败 - " + salesChannces);
            }
        }

        return mav;
    }

    @GetMapping("/YingXiaoGuanLi/salesChannces-modify")
    public ModelAndView salesChanncesModifyView(
            @RequestParam(name = "id",required = true) int id
    ){
        ModelAndView mav = new ModelAndView("YingXiaoGuanLi/salesChannces-modify");
        SalesChannces salesChannces = salesChanncesService.findById(id);
        mav.addObject("salesChannces",salesChannces);
        return mav;
    }



//    @GetMapping("/YingXiaoGuanLi/salesChannces-details")
//    public ModelAndView salesChanncesDetailsView(){
//        ModelAndView mav = new ModelAndView("YingXiaoGuanLi/salesChannces-details");
//        return mav;
//    }
}
